#******************************************************************
#
# Copyright 2017 Microsoft
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import os
import os.path
from tools.scons.RunTest import run_test

# Set to 1 for the test elevator client and elevator server to communicate through IoTivity stack.
use_iotivity = 0

Import('test_env')
ipcatest_env = test_env.Clone()
src_dir = ipcatest_env.get('SRC_DIR')
target_os = ipcatest_env.get('TARGET_OS')
with_cloud = ipcatest_env.get('WITH_CLOUD')

######################################################################
# Build flags
######################################################################
ipcatest_env.AppendUnique(CPPPATH=[
    '#/resource/include',
    '#/resource/oc_logger/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/csdk/security/include',
    '#/resource/csdk/security/provisioning/include',
    '#/resource/csdk/security/provisioning/include/internal',
    '#/resource/csdk/stack/include/internal',
    '#/resource/csdk/connectivity/api',
    '#/resource/csdk/connectivity/external/inc',
    '#/resource/csdk/ocsocket/include',
    '#/resource/csdk/logger/include',
    '#/resource/c_common',
    '#/resource/c_common/ocrandom/include',
    '#/resource/c_common/octhread/include',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/c_common/oic_time/include',
    '#/extlibs/boost/boost',
    '#/extlibs/libcoap/libcoap/include',
    '../inc',
    '../src/inc',
])

if target_os in ['windows', 'ios']:
    oclib = 'oc'
else:
    oclib = 'oc_internal',

ipcatest_env.PrependUnique(LIBS=[
        'oc_logger_internal',
        oclib,
        'octbstack',
        'ipca_static',
        'logger',
        'c_common',
        ])

if use_iotivity == 1:
    ipcatest_env.PrependUnique(LIBS=[
        'oc',
        'connectivity_abstraction',
        'coap',
    ])

    if ipcatest_env.get('SECURED') == '1':
        ipcatest_env.PrependUnique(LIBS=[
            'mbedtls',
            'mbedx509',
            'mbedcrypto',
            'ocprovision'
        ])

if target_os in ['msys_nt', 'windows']:
    ipcatest_env.AppendUnique(LINKFLAGS=['/subsystem:CONSOLE'])
    ipcatest_env.AppendUnique(LIBS=['win_helper'])

if ipcatest_env.get('MULTIPLE_OWNER') == '1':
    ipcatest_env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])

if with_cloud:
    ipcatest_env.AppendUnique(CPPDEFINES=['WITH_CLOUD'])

######################################################################
# Source files and Targets
######################################################################

unittests_src = [
    'ipcaunittests.cpp',
    'IPCAElevatorClient.cpp',
    'testelevatorserver.cpp',
    'testelevatorclient.cpp'
]

if use_iotivity == 0:
    unittests_src += [
        'mockOC.cpp',
        'mockInProcClientWrapper.cpp',
        'mockInProcServerWrapper.cpp',
        'mockOCPlatform_impl.cpp',
        'mockOCProvision.cpp',
    ]

if target_os not in ['windows', 'msys_nt']:
    ipcatest_env.AppendUnique(LIBS=['dl'])

if 'g++' in ipcatest_env.get('CXX'):
    ipcatest_env.AppendUnique(LIBS=['pthread'])

ipcatests = ipcatest_env.Program('ipcatests', unittests_src)
ipcatest_env.ScanJSON('resource/IPCA/unittests')

if target_os in ['windows']:
    #path to ipca.dll
    ipcatest_env.AppendENVPath('PATH', ipcatest_env.get('BUILD_DIR'))

if ipcatest_env.get('TEST') == '1':
    if target_os in ['windows']:
        run_test(ipcatest_env, 'resource_ipca_unittests.memcheck',
                 'resource/ipca/unittests/ipcatests', ipcatests)
